
const {
  baseUrl, appid
} = require("./config.js");
//app.js
/* 
最初进入小程序
*/
App({
  onLaunch() {
      this.isItineraryInfoByOpenId()
  },
  onHide(){
    this.unbindIdentity()
  },
  globalData: {
    openId: '',
    isItineraryInfoByOpenId: false,
    switchId:false,
  },
  // 根据openID判断是否有今日行程数据
  isItineraryInfoByOpenId() {
    let that = this
    wx.login({
      success: (result) => {
        wx.showLoading({
          title: '获取默认行程中',
          icon: 'loading',
          mask: true,
        })
        wx.request({
          method: 'get',
          url: baseUrl + '/weChat/isItineraryInfoByOpenId',
          data: {
            code: result.code,
            appId: appid,
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            wx.hideLoading()
            if (res.data.success == true) {
              let resData = JSON.parse(res.data.data)
              if (resData.success == true) {
                that.globalData.openId = resData.data.openId
                that.globalData.isItineraryInfoByOpenId = resData.data.isItineraryInfoByOpenId
                if (that.openIdParamCallback) {
                  that.openIdParamCallback(resData.data)
                }
              }
            }
          },
          fail(err) {
            wx.hideLoading()
          },
        })
      },
    })
  },

   // 解除绑定，页面返回，并通知后台，解除绑定可以把数据修复回来
   unbindIdentity: function () {
    let that = this
    Request.post('/weChat/unbindIdentity?phone=' + that.data.phone, //调用方法
        {
            'content-type': 'application/json', // 默认值
        },
    ).then(res => { //成功回调
        if (res.data.success == true) {
            wx.redirectTo({
                url: '/pages/index/index?isUnbind=1',
              })
            wx.showToast({
                title: '解绑成功',
                icon: 'none',
                duration: 2000
            })
        }
    }).catch(err => { }); //异常回调
},
})